<%--
  Created by IntelliJ IDEA.
  User: 陌·笙
  Date: 2018-12-14
  Time: 15:00
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<html>
<head>
    <title>库存查询</title>
    <jsp:include page="../comm/tag.jsp"/>
    <script>
        $(function () {
            $.ajax({
                type:"post",
                url:"/kucun/totalbaojing",
                success:function (data) {
                    if(data>0){
                        swal({
                            title: "有商品报警了！",
                            text: "是否需要查看",
                            type: "warning",
                            showCancelButton: true,
                            confirmButtonColor: "#DD6B55",
                            confirmButtonText: "查看",
                            closeOnConfirm: false
                        }, function () {
                            window.open('/kucun/baojing.jsp');
                            /*location.href="";*/
                        })
                    }
                }
            })
            $("#mytable").bootstrapTable({
                url:"/kucun/showRepertory",
                height:730,
                pageSize:12,
                pageNumber:1,
                pageList:[4,8,10,12],
                //启用分页，但是默认情况下，是客户端分页的
                pagination:true,
                sidePagination:'server',
                queryParams:function(params){
                    var temp = {  //这里的键的名字和控制器的变量名必须一直，这边改动，控制器也需要改成一样的
                        limit: params.limit,  //页面大小
                        offset: params.offset, //页码
                        repertoryName:$("[name='repertoryName']").val(),
                        typeId:$("[name='typeId']").val(),
                        firmId:$("[name='firmId']").val(),
                    };
                    return temp;
                },
                locale:'zh-CN',//中文支持,
                toolbar:'#top',
                columns:[
                    {field:"repertoryId",title:"商品编号",align:"center"},
                    {field:"repertoryName",title:"商品名称",align:"center"},
                    {field:"model",title:"手机型号",align:"center"},
                    {field:"type.iptypeName",title:"手机类别",align:"center"},
                    {field:"repertoryNum",title:"商品数量",align:"center"},
                    {field:"presetPrice",title:"预设价钱",align:"center"},
                    {field:"price",title:"进价",align:"center"},
                    {field:"total",title:"销售总估值",align:"center"},
                    {field:"li",title:"利润率",align:"center"},
                    {field:"repertoryColor",title:"商品颜色",align:"center"},
                    {field:"repertoryRemark",title:"商品备注",align:"center"},
                    {field:"firm.firmName",title:"生产厂商",align:"center"},
                    {field:"lowest",title:"最低库存",align:"center"},
                    {
                        title:"操作",
                        align:"center",
                        formatter:function(value,row,index){
                            return " <button class=\"btn btn-primary\" onclick=\"up('"+index+"')\" > <span class=\"glyphicon glyphicon-wrench\"></span>&nbsp;&nbsp;修改进价</button> <button class=\"btn btn-danger\" onclick=\"lowest('"+index+"')\" > <span class=\"glyphicon glyphicon-edit\"></span>&nbsp;&nbsp;最低库存</button>"
                        }
                    },
                ]
            });
            $.ajax({
                url:"/role/showfirm",
                type:"get",
                success:function(data){
                    $.each(data,function(){
                        var adds=$("<option value='"+this.firmId+"'>"+this.firmName+"</option>");
                        $(".s").append(adds);
                    });
                }
            })
            $.ajax({
                url:"/type/showType",
                type:"get",
                success:function(data){
                    $.each(data,function(){
                        var adds=$("<option value='"+this.iptypeid+"'>"+this.iptypeName+"</option>");
                        $(".k").append(adds);
                    });
                }
            })
        })
        function up(index){
            $("#mytable").bootstrapTable("check",index);//根据索引让该行选中
            var a=$("#mytable").bootstrapTable("getSelections")[0];//取出选中的第一行数据
            $("#modal-id").modal('show');
            $(".names").html(a.repertoryName);
            $("[name='repertoryId']").val(a.repertoryId);
            $("[name='price']").val(a.price);
            $("[name='tableId']").val(index);
        }
        function  cha(){//查询
            var opts = $("#mytable").bootstrapTable("getOptions") ;
            var repertoryName=$("[name='repertoryName']").val();
            var typeId=$("[name='typeId']").val();
            var firmId=$("[name='firmId']").val();
            var limit = opts.pageSize ;
            var offset = (opts.pageNumber-1) * limit;
            $.ajax({
                url:"/kucun/showRepertory",
                type:"get",
                data:{"repertoryName":repertoryName,"typeId":typeId,'firmId':firmId,'limit':limit,'offset':offset},
                success:function(data){
                    $("#mytable").bootstrapTable("load",data);
                }
            })
        }
        function lowest(index){
            $("#mytable").bootstrapTable("check",index);//根据索引让该行选中
            var a=$("#mytable").bootstrapTable("getSelections")[0];//取出选中的第一行数据
            $("#zuidi").modal('show');
            $(".repertoryName").html(a.repertoryName);
            $(".repertoryNum").html(a.repertoryNum);
            $("[name='lowest']").val(a.lowest);
            $("[name='tableId']").val(index);
            $("[name='repertoryId']").val(a.repertoryId);
        }
        $(function(){//关闭所有的模态框
            $(".closes").on("click",function () {
                //关闭对话框
                $("#modal-id").modal('hide');
                $("#zuidi").modal('hide');
                //清空表单
                $('#myform')[0].reset();
                $("#mytable").bootstrapTable("uncheck",$("[name='tableId']").val());//取消选中
            })
        })
        function smt() {
            //序列化表单
            var formStr = $('#uplowest').serialize();
            //处理表单控件的中文乱码
            repertory = decodeURIComponent(formStr, true);
            $.ajax({
                url:"/kucun/uplowert",
                data:repertory,
                type:"post",
                success:function (data) {
                    $("#mytable").bootstrapTable('load', data);
                    $("#mytable").bootstrapTable("uncheck",$("[name='tableId']").val());//取消选中
                    $("#zuidi").modal('hide');
                }

            })
        }
    </script>
</head>
<body>
<div style="margin-left: 50px;margin-right: 50px">
    <div class="#top" style="margin-top: 30px">
        <div class="col-md-3">
           &nbsp;&nbsp; <button onclick="todel()" class="btn btn-primary"><span class="glyphicon glyphicon-save"></span>&nbsp;&nbsp;导出</button>
        </div>
        <div class="col-md-9">
            <form action="" method="post" role="form" class="form-horizontal">
                <div class="form-group col-md-4">
                    <label for="" class="col-md-4 control-label">商品名称:</label>
                    <div class="col-md-8">
                        <input type="text" class="form-control" name="repertoryName" placeholder="可模糊查询">
                    </div>
                </div>

                <div class="form-group col-md-4">
                    <label for="" class="col-md-4 control-label">商品类别:</label>
                    <div class="col-md-8">
                        <select name="typeId" id="inputID" class="form-control k">
                            <option value="0">全部</option>
                        </select>
                    </div>
                </div>
                <div class="form-group col-md-4">
                    <label for="" class="col-md-4 control-label">厂商:</label>
                    <div class="col-md-8">
                        <select name="firmId" id="firm" class="form-control s">
                            <option value="0">全部</option>
                        </select>
                    </div>
                </div>
                <button type="button" class="btn btn-primary btn-info glyphicon glyphicon-search" onclick="cha()">查询</button>
            </form>
        </div><%--搜索框--%>
    </div><%--搜索框--%>
 <table id="mytable"></table>
    <div class="modal fade" id="modal-id">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                    <h4 class="modal-title">修改进价</h4>
                </div>
                <div class="modal-body">
                    <form action="" method="post" id="myform" role="form" class="form-horizontal">
                        <div class="form-group">
                            <label for="" class="col-md-2">手机名称:</label>
                            <label for="" class="col-md-10 names"></label>
                        </div>
                        <div class="form-group">
                            <label for="" class="col-md-2">手机进价:</label>
                            <div class="col-md-9">
                                <input type="hidden" class="form-control" name="repertoryId" placeholder="编号">
                                <input type="text" class="form-control" name="price" placeholder="手机进价">
                                <input type="hidden" class="form-control" name="tableId" placeholder="手机进价">
                            </div>
                        </div>
                    </form>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default closes">取消</button>
                    <button type="button" class="btn btn-primary" onclick="upprice()">保存</button>
                </div>
            </div><!-- /.modal-content -->
        </div><!-- /.modal-dialog -->
    </div><!-- 修改进价 -->
    <div class="modal fade" id="zuidi">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                    <h4 class="modal-title">设置最低库存</h4>
                </div>
                <div class="modal-body">
                    <form action="" method="post" id="uplowest" role="form" class="form-horizontal">
                        <div class="form-group">
                            <label for="" class="col-md-4">商品名称:&nbsp;&nbsp;<span class="repertoryName"></span></label>
                            <input type="hidden" class="form-control" name="repertoryId" placeholder="编号">
                        </div>
                        <div class="form-group">
                            <label for="" class="col-md-3">当前数量:&nbsp;&nbsp;<span class="repertoryNum"></span></label>
                        </div>
                        <div class="form-group">
                            <label for="" class="col-md-2">最低库存:</label>
                           <div class="col-md-10">
                               <input type="text" class="form-control" name="lowest" placeholder="最低库存">
                               <input type="hidden" class="form-control" name="tableId" placeholder="选中的table复选框">
                           </div>
                        </div>
                    </form>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default closes">取消</button>
                    <button type="button" class="btn btn-primary" onclick="smt()">保存</button>
                </div>
            </div><!-- /.modal-content -->
        </div><!-- /.modal-dialog -->
    </div><!-- /.modal -->
</div>
<script>
    function upprice() {
        swal({
            title: "您确定要修改进价吗？",
            text: "请谨慎操作！",
            type: "warning",
            showCancelButton: true,
            confirmButtonColor: "#DD6B55",
            confirmButtonText: "审核",
            closeOnConfirm: false
        }, function () {
            var opts = $("#mytable").bootstrapTable("getOptions") ;
            var repertoryName=$("[name='repertoryName']").val();
            var typeId=$("[name='typeId']").val();
            var firmId=$("[name='firmId']").val();
            var limit = opts.pageSize ;
            var offset = (opts.pageNumber-1) * limit;
            var price=$("[name='price']").val();
            var repertoryId=$("[name='repertoryId']").val();
            $.ajax({
                url:"/kucun/upprice",
                type:"post",
                data:{"price":price,"repertoryId":repertoryId,"repertoryName":repertoryName,"typeId":typeId,'firmId':firmId,'limit':limit,'offset':offset},
                success:function(data){
                    $("#mytable").bootstrapTable("load",data);
                    $("#modal-id").modal('hide');
                }
            })
            swal("修改成功！", "该商品进价修改成功", "success");
        });
    };
</script>
</body>
</html>
